package org.redhat.auth.security.util;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

/**
 * 
 * @author lingen.liu Resource资源管理
 */
public class ResourceCacheUtil {

	private static Cache cache;

	/**
	 * 清除所有的URL资源缓存
	 */
	public static void removeAllResourceCache() {
		getCache().removeAll();
	}

	/**
	 * 缓存更新，更新指定URL的缓存
	 * 
	 * @param url
	 * @param roles
	 */
	public static void updateUrlRoles(String url, String[] roles) {
		Collection<ConfigAttribute> cas = new ArrayList<ConfigAttribute>();
		for (String role : roles) {
			ConfigAttribute ca = new SecurityConfig(role);
			cas.add(ca);
		}
		Element element = new Element(url, cas);
		getCache().put(element);
	}

	private static Cache getCache() {
		if (cache == null)
			cache = CacheManagerUtil.getResourceCache();
		return cache;
	}
}
